# -*- coding: UTF-8 -*-
'''
@Project ：pythonProject 
@File    ：strDemo5.py
@Author  ：siwei
@Date    ：2023/10/25 21:01 
@other   ：str.print---->print(str)
'''
#字符串的编码和解码
#byte类型:二级制数据
#计算机A传输到计算机B:str--->bytes--->str
#编码:str--->bytes        str.encode()
#encoding是编码格式,errors是错误处理方式:strict抛出错误/ignore忽略/replace用?代替
str = '伟大的中国梦'
scode = str.encode(encoding='utf-8', errors='replace')  #utf-8格式时,中文占3个字节
print(scode)
scode_gbk = str.encode(encoding='gbk', errors='replace')    #gbk格式时,中文占2个字节
print(scode_gbk)

#出错问题
str1 = '耶🍏'
scode_replace = str1.encode(encoding='utf-8', errors='replace') #replace:用?代替
print(scode_replace)
scode_ignore = str1.encode(encoding='utf-8', errors='ignore') #ignore:忽略
print(scode_ignore)
scode_strict = str1.encode(encoding='utf-8', errors='strict') #strict:抛出错误
print(scode_strict)

#解码:bytes--->str        bytes.decode()
# bytes.decode(encoding='utf-8', errors='strict/ignore/replace')
#编码和解码的encoding需要一一对应
print(bytes.decode(scode,'utf-8'))
print(bytes.decode(scode_gbk,'gbk'))
print(bytes.decode(scode_strict,'utf-8'))